﻿


#pragma once

#include <d3dx9-dynamic-load/d3dx9_dynamic_load.h>

#include <stdlib.h>
#include <stdarg.h>

#include <assert.h>


#pragma warning(push)
#pragma warning(disable : 4290)
 

namespace d3dx9_dynamic_load
{


 struct font_create_options
 {

		 INT Height;
		 UINT Width;	
		 UINT Weight;
		 UINT MipLevels;
		 BOOL Italic;
		 DWORD CharSet;
		 DWORD OutputPrecision;
		 DWORD Quality;
		 DWORD PitchAndFamily;
		 CHAR  Facename [32];


 
	 font_create_options()
	 {
		memset(&Height,0,sizeof(font_create_options) );

		Height=15;
		Width=0;	
		Weight=FW_BOLD;
		MipLevels=1;
		Italic=FALSE;
		CharSet=DEFAULT_CHARSET;
		OutputPrecision=OUT_DEFAULT_PRECIS;
		Quality=DEFAULT_QUALITY;
		PitchAndFamily=DEFAULT_PITCH | FF_DONTCARE;
		SetFontName( "Arial" );
 

	 }

	 inline void SetFontName(const char* _fontName)
	 {
		 // strncpy(Facename, fontName, 31 );
		 int c=0;
		 for(c=0; c<31; c++)
		 {
			if(_fontName[c] )
			{
				Facename[c] = _fontName[c];
				continue;
			}

			break;
		 }

		 Facename[c+1] = '\0';
		 
	 }
 };


 
	 
 inline ID3DXFont* const CreateFontA(
			IDirect3DDevice9* const device, 
			CD3DX9_Dll_Loader* const loader,
			const font_create_options& opt
			) throw(HRESULT)
 {
	ID3DXFont* res = NULL;

	HRESULT hr = loader->D3DXCreateFontA(  device,	
	opt.Height,	opt.Width,	opt.Weight, opt.MipLevels,
	opt.Italic, opt.CharSet, opt.OutputPrecision, 
	opt.Quality, opt.PitchAndFamily, opt.Facename,	
	&res );

	assert(res);
	if FAILED(hr) throw(hr);

	return res;
 }



class Font {
private:
	mutable INT m_lastResult;

public:
	ID3DXFont* const m_pfont;
	int m_posx;
	int m_posy;
	D3DCOLOR m_color;
	DWORD m_format;


	Font(IDirect3DDevice9* const device, 
		CD3DX9_Dll_Loader* const loader,
		const font_create_options& opt)	: m_pfont(CreateFontA(device, loader, opt) )
	{
		 m_posx = 10;
		 m_posy = 10;
		 m_color = 0xffffffff;
		 m_format = DT_LEFT | DT_TOP   ;
		 m_lastResult = 1;
	}

	virtual ~Font()
	{
		if(m_pfont)
		{
			m_pfont->Release();
			{
				ID3DXFont** pp = const_cast<ID3DXFont**>(&m_pfont);
				*pp = NULL;
			}
		}
	}

	inline  void SetColor(const D3DCOLOR value)
	{
		m_color = value;
	}

	inline void SetColor(byte_ r, byte_ g, byte_ b, byte_ a)
	{
		D3DCOLOR temp = (((DWORD)a)<<24)|(((DWORD)r)<<16)|(((DWORD)g)<<8)|(((DWORD)b));
		m_color = temp;
	}

	inline void SetColorAlpha(byte_ a)
	{
		m_color |= 0xff000000;
		m_color ^= 0xff000000;
		m_color |= (((D3DCOLOR)a)<<24);
	}

 
	inline void SetColorRed()
	{
		m_color = 0xffff0000;
	}

	inline void SetColorGreen()
	{
		m_color = 0xff00ff00;
	}

	inline void SetColorBlue()
	{
		m_color = 0xff0000ff;
	}


	inline void SetColorPink()
	{
		m_color = 0xffff00ff;
	}

	inline void SetColorYellow()
	{
		m_color = 0xffffff00;
	}

	inline void SetColorAqua()
	{
		m_color = 0xff00ffff;
	}

	inline void SetColorDark()
	{
		m_color = 0xff7f7f7f;
	}




	//---------------------------------------------------------

	void SetPosition(int x, int y)
	{
		m_posx = x;
		m_posy = y;
	}

    HRESULT DrawStrA(const char* str ) const;
 
	HRESULT DrawStrW(const WCHAR* str) const ;


    HRESULT DrawFormatA(const char*  _Format, ... ) const;
    HRESULT DrawFormatW(const WCHAR* _Format, ... ) const;


	inline INT GetLastResult() const
	{
		return m_lastResult;
	}
	 


private:

	Font(const Font& a) :  m_pfont(NULL) 
	{
		assert(false);
	}
};



	namespace test
	{
		void do_test_drawfont(Font* const);
	}


}


#pragma warning(pop)
